# AI-Offer 后端项目 Makefile

# 项目名称
PROJECT_NAME := ai-offer
# 可执行文件存放路径
BIN_DIR := build/bin
# 源代码路径
SRC_DIR := .
# 主要API服务
API_MAIN := cmd/api/main.go
# 编译输出
API_BIN := $(BIN_DIR)/api-server

# 版本信息
VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "unknown")
BUILD_DATE := $(shell date '+%Y-%m-%d %H:%M:%S')
GIT_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
GIT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown")

# 编译标签
GO_LDFLAGS := -ldflags "-X 'main.Version=$(VERSION)' -X 'main.BuildDate=$(BUILD_DATE)' -X 'main.GitCommit=$(GIT_COMMIT)' -X 'main.GitBranch=$(GIT_BRANCH)'"

# 基本命令
GO := go
GOMOD := $(GO) mod
GOBUILD := $(GO) build $(GO_LDFLAGS)
GOTEST := $(GO) test
GOGET := $(GO) get
GOCLEAN := $(GO) clean
GOVET := $(GO) vet
GOFMT := gofmt -s -w
GOLINT := golangci-lint

# Docker相关
DOCKER := docker
DOCKER_COMPOSE := docker-compose
DOCKER_FILE := Dockerfile
DOCKER_FILE_DEV := Dockerfile.dev
DOCKER_IMAGE := ai-offer-backend
DOCKER_TAG := $(VERSION)

# 代码生成相关
GOCTL := goctl
API_DIR := api/desc
API_FILE := $(API_DIR)/user.api
GEN_DIR := internal

# 数据库相关
DB_CONFIG := etc/api.yaml
MIGRATE_DIR := schema.sql

# 平台支持
PLATFORMS := linux/amd64 darwin/amd64 windows/amd64

.PHONY: all build build-all clean test test-race test-cover run lint fmt vet docker docker-dev api-gen help db-init mod-tidy version bench

# 默认目标：构建
all: mod-tidy fmt vet build

# 编译构建
build:
	@echo "Building $(PROJECT_NAME) API server..."
	@mkdir -p $(BIN_DIR)
	$(GOBUILD) -o $(API_BIN) $(API_MAIN)
	@echo "Build complete. Binary location: $(API_BIN)"

# 编译构建所有平台
build-all:
	@echo "Building $(PROJECT_NAME) for multiple platforms..."
	@mkdir -p $(BIN_DIR)
	@$(foreach platform,$(PLATFORMS),\
		echo "Building for $(platform)..." && \
		GOOS=$(firstword $(subst /, ,$(platform))) \
		GOARCH=$(lastword $(subst /, ,$(platform))) \
		$(GOBUILD) -o $(BIN_DIR)/$(PROJECT_NAME)-$(firstword $(subst /, ,$(platform)))-$(lastword $(subst /, ,$(platform))) $(API_MAIN) && \
		echo "Done for $(platform)" && \
	) true
	@echo "Build complete for all platforms. Binaries location: $(BIN_DIR)"

# 编译并运行
run: build
	@echo "Running $(PROJECT_NAME) API server..."
	$(API_BIN) -f etc/api.yaml

# 清理构建产物
clean:
	@echo "Cleaning build artifacts..."
	$(GOCLEAN)
	@rm -rf $(BIN_DIR)
	@echo "Clean complete"

# 运行测试
test:
	@echo "Running tests..."
	$(GOTEST) ./... -v
	@echo "Tests complete"

# 运行竞态检测测试
test-race:
	@echo "Running tests with race detector..."
	$(GOTEST) -race ./... -v
	@echo "Race tests complete"

# 运行测试覆盖率分析
test-cover:
	@echo "Running tests with coverage analysis..."
	$(GOTEST) -coverprofile=coverage.out ./...
	$(GO) tool cover -html=coverage.out -o coverage.html
	@echo "Coverage analysis complete. See coverage.html for details."

# 性能基准测试
bench:
	@echo "Running benchmark tests..."
	$(GOTEST) -bench=. -benchmem ./...
	@echo "Benchmark tests complete"

# 运行lint检查
lint:
	@echo "Running linter..."
	$(GOLINT) run ./...

# 格式化代码
fmt:
	@echo "Formatting code..."
	$(GOFMT) ./cmd ./internal ./api

# 代码静态检查
vet:
	@echo "Checking code with go vet..."
	$(GOVET) ./...

# 检查依赖并更新
mod-tidy:
	@echo "Tidying and verifying module dependencies..."
	$(GOMOD) tidy
	$(GOMOD) verify

# 构建生产Docker镜像
docker:
	@echo "Building production Docker image..."
	$(DOCKER) build -t $(DOCKER_IMAGE):$(DOCKER_TAG) -f $(DOCKER_FILE) .
	$(DOCKER) tag $(DOCKER_IMAGE):$(DOCKER_TAG) $(DOCKER_IMAGE):latest

# 构建开发Docker镜像
docker-dev:
	@echo "Building development Docker image..."
	$(DOCKER) build -t $(DOCKER_IMAGE):dev -f $(DOCKER_FILE_DEV) .

# 启动Docker开发环境
docker-run-dev:
	@echo "Starting Docker development environment..."
	$(DOCKER_COMPOSE) up -d

# 通过API定义生成代码
api-gen:
	@echo "Generating code from API definitions..."
	$(GOCTL) api go -api $(API_FILE) -dir .

# 初始化数据库
db-init:
	@echo "Initializing database..."
	@# 可以添加实际的数据库初始化命令，例如使用psql或其他工具执行schema.sql

# 显示版本信息
version:
	@echo "$(PROJECT_NAME) Version: $(VERSION)"
	@echo "Build Date: $(BUILD_DATE)"
	@echo "Git Commit: $(GIT_COMMIT)"
	@echo "Git Branch: $(GIT_BRANCH)"

# 帮助信息
help:
	@echo "Available targets:"
	@echo "  all          - Update dependencies, format code, check and build"
	@echo "  build        - Build the project"
	@echo "  build-all    - Build for multiple platforms ($(PLATFORMS))"
	@echo "  clean        - Remove build artifacts"
	@echo "  test         - Run tests"
	@echo "  test-race    - Run tests with race detector"
	@echo "  test-cover   - Run tests with coverage analysis"
	@echo "  bench        - Run benchmark tests"
	@echo "  run          - Build and run the application"
	@echo "  lint         - Run linter"
	@echo "  fmt          - Format code"
	@echo "  vet          - Run go vet"
	@echo "  mod-tidy     - Tidy and verify dependencies"
	@echo "  docker       - Build production Docker image"
	@echo "  docker-dev   - Build development Docker image"
	@echo "  docker-run-dev - Start Docker development environment"
	@echo "  api-gen      - Generate code from API definitions"
	@echo "  db-init      - Initialize database"
	@echo "  version      - Show version information"
	@echo "  help         - Show this help message" 